// pages/my/carbonCreditDetail/index.js
import * as echarts from '../../../ec-canvas/echarts';

var thisMonthData = [75, 90, 80, 90, 35]
var lastMonthData = [65, 60, 70, 65, 55]

function initChart(canvas, width, height, dpr) {
  console.log(thisMonthData)
  console.log(lastMonthData)
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // new
  });
  canvas.setChart(chart);

  var option = {
    backgroundColor: "#ffffff",
    xAxis: {
      show: false
    },
    yAxis: {
      show: false
    },
    radar: {
      // shape: 'circle',
      indicator: [{
        name: '碳信用',
        max: 100
      },
      {
        name: '步数',
        max: 100
      },
      {
        name: '骑行',
        max: 100
      },
      {
        name: '餐具',
        max: 100
      },
      {
        name: '公交',
        max: 100
      }
      ]
    },
    series: [{
      name: '预算 vs 开销',
      type: 'radar',
      data: [{
        value: lastMonthData,
        name: '预算'
      },
      {
        value: thisMonthData,
        name: '开销'
      }
      ]
    }]
  };

  chart.setOption(option);
  return chart;
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    ec: {
      onInit: initChart
    },
    new: [
      "2022-03-19",
      "58.0",
      "66.67%",
      "92.23%",
      "82.02%",
      "79.53%",
      "22.67%"
    ],
    old: [
      "2022-03-19",
      "71.0",
      "100.0%",
      "100.0%",
      "66.67%",
      "100.0%",
      "100.0%"
    ],
    show: true,
    newData: [
      {
        name: '碳信用',
        value: '66%',
        imageUrl: '../../../image/temp/Saly-5.png'
      },
      {
        name: '步行减碳',
        value: '92%',
        imageUrl: '../../../image/temp/Saly-2.png'
      },
      {
        name: '骑行减碳',
        value: '82%',
        imageUrl: '../../../image/temp/Saly-3.png'
      },
      {
        name: '餐具减碳',
        value: '79.53%',
        imageUrl: '../../../image/temp/Saly-4.png'
      },
      {
        name: '公交减碳',
        value: '22.67%',
        imageUrl: '../../../image/temp/Saly-1.png'
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.request({
      url: 'http://127.0.0.1:8000/credits/',
      method: "GET",
      headers: {
        'Content-Type': 'application/json'
      },
      data: {
        user_id: getApp().globalData.user.userId
      },
      success: function (res) {
        console.log(res.data.old)
        console.log(res.data.new)

        if (res.statusCode == 500) {
          wx.showToast({
            title: '获取失败',
            icon: 'error',
            duration: 2000
          })
        } else {
          that.setData({
            old: res.data.old,
            new: res.data.new
          })

          that.toNewData()
          that.toPoint()
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '连接失败',
          icon: 'error',
          duration: 2000
        })
      }
    })
  },

  /**
   * 转化为显示的格式
   */
  toNewData() {
    let temp = this.data.newData
    for (let i = 2; i < 7; i++) {
      temp[i - 2].value = this.data.new[i]
    }
    this.setData({
      newData: temp
    })
  },

  /**
   * 转化
   */
  toPoint() {
    for (var i = 2; i < this.data.old.length; i++) {
      lastMonthData[i - 2] = this.data.old[i].replace("%", "")
    }

    for (var i = 2; i < this.data.new.length; i++) {
      thisMonthData[i - 2] = this.data.new[i].replace("%", "")
    }

    console.log(lastMonthData)
    console.log(thisMonthData)

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})